/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午3:17:00
 * BaseDao.java
 * 
 */
package com.fujitsu.nanjing.mobile.dao;

import com.fujitsu.nanjing.mobile.exception.NotOneException;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * @author qixb.fnst
 * 
 */
public class BaseDao<T> extends HibernateDaoSupport {

    private final static DateFormat dateFormat = new SimpleDateFormat("HH:mm");
    private final static DateFormat dateFormatWithDay = new SimpleDateFormat(
            "yyyy-MM-dd");
    private final static DateFormat dateFormatWithDayAndTime = new SimpleDateFormat(
            "MM-dd HH:mm");

    public T insert(T t) {
        getHibernateTemplate().saveOrUpdate(t);
        getHibernateTemplate().flush();
        return t;
    }

    public T update(T t) {
        getHibernateTemplate().update(t);
        getHibernateTemplate().flush();
        return t;
    }

    public static DateFormat getDateformat() {
        return dateFormat;
    }

    public static String getDateformatString(Date date) {
        if (date == null) {
            return null;
        }
        return dateFormat.format(date);
    }

    public static String getDateformatWithDayAndTimeString(Date date) {
        if (date == null) {
            return null;
        }
        return dateFormatWithDayAndTime.format(date);
    }

    public static String getDateformatWithDayString(Date date) {
        if (date == null) {
            return null;
        }
        return dateFormatWithDay.format(date);
    }

    public static DateFormat getDateformatwithday() {
        return dateFormatWithDay;
    }

    public void delete(T t) {
        getHibernateTemplate().delete(t);
        getHibernateTemplate().flush();
    }

    public T query(Class<T> clazz, int id) {
        return (T) getHibernateTemplate().get(clazz, id);

    }

    @SuppressWarnings("unchecked")
    public List<T> query(String queryString) {
        return getHibernateTemplate().find(queryString);
    }

    @Resource
    public void setSessionFacotry(SessionFactory sessionFacotry) {
        super.setSessionFactory(sessionFacotry);
    }

    @SuppressWarnings("unchecked")
    protected List<T> query(Criteria criteria) {
        return criteria.list();
    }

    @SuppressWarnings("unchecked")
    protected T query4One(Criteria criteria) throws NotOneException {
        List<T> list = criteria.list();
        if (list == null || list.size() == 0) {
            throw new NotOneException("没有查询到结果集！");
        }
        if (list.size() > 1) {
            throw new NotOneException("数据库数据出错！");
        }
        return list.get(0);
    }

    protected int queryNums(Class<T> t, String whereClause) {
        StringBuilder sb = new StringBuilder();
        sb.append("select count(*) from ").append(t.getSimpleName());
        if (whereClause != null) {
            sb.append(" where ").append(whereClause);
        }
        String hql = sb.toString();
        Session session = getSession();
        Number count = (Number) session.createQuery(hql).uniqueResult();
        releaseSession(session);
        return count.intValue();
    }

    /**
     * 
     * <font color='red'>该方法只支持mysql</font>
     * ***/
    protected List<T> query4Page(Class<T> t, String orderClause,
            String whereClause, boolean userLimit, int firstRecord, int limit) {
        StringBuilder sb = new StringBuilder();
        sb.append(" from ").append(t.getSimpleName());
        if (whereClause != null) {
            sb.append(" where ").append(whereClause);
        }
        if (orderClause != null) {
            sb.append(" order by").append(orderClause);
        }
        Session session = getSession();
        Query query = session.createQuery(sb.toString());
        if (userLimit) {
            query.setFirstResult(firstRecord);
            query.setMaxResults(limit);
        }
        @SuppressWarnings("unchecked")
        List<T> ts = query.list();
        releaseSession(session);
        return ts;
    }
}
